package com.spring.beans.factory;

import java.lang.reflect.Method;

import com.spring.beans.BeanDefinition;
import com.spring.beans.BeanReference;
import com.spring.beans.PropertyValue;

public class AutowireCapableBeanFactory extends AbstractBeanFactory {

	@Override
	protected void applyPropertyValues(Object bean, BeanDefinition beanDefinition) {

		for (PropertyValue propertyValue : beanDefinition.getPropertyValues().getPropertyValues()) {
			Object value = propertyValue.getValue();
			try {
				// 如果是引用Bean 直接查找
				if (value instanceof BeanReference) {
					BeanReference beanReference = (BeanReference) value;
					getBean(beanReference.getName());
				}
				String methodName = "set" + propertyValue.getName().substring(0, 1).toUpperCase()
						+ propertyValue.getName().substring(1);
				Method declaredMethod = bean.getClass().getDeclaredMethod(methodName,value.getClass());
				declaredMethod.setAccessible(true);
				declaredMethod.invoke(bean, value);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
